package school.xauat.inscription_sys.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import school.xauat.inscription_sys.pojo.User;
import school.xauat.inscription_sys.pojo.vo.RespVo;
import school.xauat.inscription_sys.service.UserService;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import static school.xauat.inscription_sys.errorcode.ErrorCode.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsy
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/inscription_sys/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public RespVo login(@RequestBody User user, HttpSession session) {
        if (user.getUsername() == null || user.getPassword() == null) {
            return RespVo.error(ERROR_USERNAME_PASSWORD_NIL, getMsg(ERROR_USERNAME_PASSWORD_NIL));
        }
        int code = userService.checkUser(user.getUsername(), user.getPassword());
        if (code == SUCCESS) {
            session.setAttribute("user", user);
            return RespVo.ok("登录成功");
        }
        return RespVo.error(code, getMsg(code));
    }

}

